package com.wanshi.servlet;

import com.wanshi.dao.UserDao;
import com.wanshi.entity.User;
import com.wanshi.service.UserService;
import com.wanshi.utils.MD5Util;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

@WebServlet("/regist")
public class RegistServlet extends HttpServlet {

    private UserService userService = new UserService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1 校验
        // 验证码
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");


        HttpSession session = request.getSession();
        String ma = (String) session.getAttribute("ma");

        String yan = request.getParameter("yan");

        if(ma == null || !ma.equals(yan)){
            request.setAttribute("error","验证码错误");
            request.getRequestDispatcher("/regist.jsp").forward(request,response);
            return;
        }

        //2 校验 用户的各个属性都不能为空  格式 用户名 6-20 字母数字下划线
        try {

            User user = new User();
            //前提 表单中表单元素的name属性值 和 对象的属性一致
            BeanUtils.copyProperties(user,request.getParameterMap());

            //2 校验 用户的各个属性都不能为空  格式 用户名 6-20 字母数字下划线

            //System.out.println(user.toString());
            Map<String, String> errorMap = user.checkUser();
            if(errorMap.size() > 0){
                request.setAttribute("map",errorMap);
                request.getRequestDispatcher("/regist.jsp").forward(request,response);
                return;
            }
            user.setPassword(MD5Util.getMD5Str(user.getPassword()));
            String[] hobbies = request.getParameterValues("hobby");


            //System.out.println("----------"+username);
            //1 单独处理爱好
            //篮球,足球,音乐
            StringBuffer buffer = new StringBuffer();
            for(String hobby:hobbies){
                buffer.append(hobby+",");
            }
            String hobby = buffer.substring(0,buffer.length()-1);
            user.setHobby(hobby);

            System.out.println(user);




            if(userService.findUserByName(user.getUsername()) != null){
                //response.getWriter().write("该用户名已经存在!");
                request.setAttribute("error","该用户名已经存在!");
                request.getRequestDispatcher("/regist.jsp").forward(request,response);
            }else{

                //2 添加到数据库
                userService.addUser(user);
                response.getWriter().write("注册成功!3秒后跳转到首页");
                response.setHeader("refresh","3;url="+request.getContextPath()+"/index.jsp");
                //3 用户名不能重复
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

